package com.yitao.cms.constant;


import com.skipper.base.bean.DataOutput;
import com.yitao.cms.model.operationUser.OperationUserCmsModel;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author wwl
 * @date: 2018/6/4 0004
 * Function:
 */
public class UserSession {
    public static final String SESSION_OPERATION_USER = "OPERATION_USER";
    public static final Integer SESSION_USER_MAX_TIME = 60 * 60 * 24; // 登录用户过期时间,一天

    /**
     * 获取登录用户信息
     *
     * @param request
     * @return
     */
    public static OperationUserCmsModel getUserSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        OperationUserCmsModel operationUserCmsModel = (OperationUserCmsModel) session.getAttribute(SESSION_OPERATION_USER);
        //给出用户登录默认值
        return operationUserCmsModel;
    }

    /**
     * 获取登录id
     * @param request
     * @return
     */
    public static Long getUserIdBySession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        OperationUserCmsModel operationUserCmsModel = (OperationUserCmsModel) session.getAttribute(SESSION_OPERATION_USER);
        //给出用户登录默认值
        if (operationUserCmsModel != null) {
           return operationUserCmsModel.getId();
        }else{
            return -1L;
        }
    }

    /**
     * 用户登出
     *
     * @param request
     * @return
     */
    public static DataOutput removeUserssion(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute(SESSION_OPERATION_USER);
        return new DataOutput();
    }
}
